<?php
/*Problem Domain
To perform the actual CRUD operations on Session by implementing the SessionDAO interface
*/
/*
include_once 'SessionDAO.php';
include_once '../../model/TrainingSession.php';
include_once '../../model/ListUserType.php';
*/
class SessionDAOImpl implements SessionDAO
{
	private $conn;
	private $array_list;

	/*void 
	purpose. initializes the newly created SessionDAOImpl.*/
	public function __construct()
	{
		$this->conn = new mysqli('localhost' , 'root' , 'root123' , 'tms') or
		die('Connect error : '.$this->conn->connect_error);

		$this->array_list = new ListUserType();
	}

	/*void -> Connection
	purpose. produces the connection object*/
	public function getConnection()
	{
		return $this->conn;
	}

	/*Session -> boolean
	purpose. produces true if the given Session is found , false otherwise*/
	private function found($Session)
	{
		$conn = $this->getConnection();
		$Session_id = $Session->getSessionName();
		$res = $conn->query("select * from Training_Session where `sess_id` = ".$Session_id);
		$status;
		
		if($res->num_rows == 0)
		{
			$status = false;
		}
		else
		{
			$status = true;
		}

		return $status;
	}

	/*Session -> boolean
	purpose. To persist the given Session into the database and return true on success
	false otherwise*/
	private function save($Session)
	{
		$conn 	   = $this->getConnection();
		$sess_name = $Session->getSessionName();
		$trainer   = $Session->getTrainer();
	   	$epoch     = $Session->getStartDateTime();
		$tech_id   = $Session->getTechId();
		$on_date   = date('Y-m-d' , $epoch);
		$start_time= date('h:i:s' , $epoch);
		$sts	   = $Session->getStatus();

		$stmt = $conn->prepare('Insert into Training_Session (`sess_name`,`trainer`,`on_date`,`start_time`,`sess_status` , `tech_id`) values (?, ?, ?, ?, ?, ?)');
		$status = true;

		if(!$stmt)
		{
			echo "\n Prepared Statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('sssssi', $sess_name, $trainer, $on_date, $start_time, $sts, $tech_id))
			{
				echo "Bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
				else
				{
					$id = $conn->insert_id;
					$Session->setId($id);
				}
			}
		}

		return $status;		
	}

	/*Session -> boolean
	pupose. To update the already existing given Session and return true on success , false otherwise.*/
	private function update($Session)
	{
		$conn       = $this->getConnection();
		$Session_id = $Session->getId();
		$sess_name  = $Session->getSessionName();
		$trainer    = $Session->getTrainer();
	   	$epoch      = $Session->getStartDateTime();
		$on_date    = date('Y-m-d' , $epoch);
		$start_time = date('h:i:s' , $epoch);
		$sts	    = $Session->getStatus();
		$status	    = true;

		$stmt = $conn->prepare('update Training_Session set sess_name = ?, trainer = ?, on_date = ?, start_time = ?, sess_status = ? 
					where sess_id = ?');

		if(!$stmt)
		{
			echo "Prepare statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('sssssi', $sess_name, $trainer, $on_date, $start_time, $sts, $Session_id))
			{
				echo "Parameter bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;
	}

	/*Session -> boolean
        purpose. to save or update a given Session into the database and return true on success , 
        false otherwise*/
        public function saveOrUpdate($Session)
	{
		$status;

		if(!$this->found($Session))
		{
			$status = $this->save($Session);			
		}
		else
		{
			$status = $this->update($Session);
		}

		return $status;
	}	

	/*Session -> boolean
        purpose. removes a given Session from the database and returns true
        on success , false otherwise*/
        public function remove($Session)
	{
		$conn = $this->getConnection();
		$status = true;

		if(!$this->found($Session))
		{
			echo "\n".$Session->getEmaiId()."is not found";
			$status = false;
		}
		else
		{
			$Session_id = $Session->getId();
			$sql     = "delete from Training_Session where sess_id = ".$Session_id;

			if(!$conn->query($sql))
			{
				echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
				$status = false;
			}
		}

		return $status;
	}
	
	/*void -> LostUserType
	purpose. produces the entire list of Sessions of the system*/
	public function listSessions()
	{
		$conn = $this->getConnection();
		$sql = 'select * from Training_Session';
		$res = $conn->query($sql);

		while($row = $res->fetch_assoc())
		{
			$sess_id 	= $row['sess_id'];
			$sess_name 	= $row['sess_name'];
			$trainer	= $row['trainer'];
			$on_date 	= $row['on_date'];
			$start_time	= $row['start_time'];
			$status		= $row['sess_status'];
			$desc		= $row['desc'];
			$tech_id	= $row['tech_id'];
			
			$Session	= new TrainingSession($sess_name, $trainer, date("$on_date $start_time"), $status, $desc, $sess_id, $tech_id);

			$this->array_list->addToList($Session);
		}

		return $this->array_list;
	}

	public function getSession($sess_id)
	{
		$conn = $this->getConnection();
		$sessn = null;
		
		$sql = "select * from Training_Session where sess_id = ".$sess_id;

		$res = $conn->query($sql);

		if(!$res)
                {
                      	echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
                }
		else
		{
			$row = $res->fetch_assoc();
		
			$sess_id        = $row['sess_id'];
                        $sess_name      = $row['sess_name'];
                        $trainer        = $row['trainer'];
                        $on_date        = $row['on_date'];
                        $start_time     = $row['start_time'];
                        $status         = $row['sess_status'];
                        $desc           = $row['desc'];
                        $tech_id        = $row['tech_id'];

                        $sessn = new TrainingSession($sess_name, $trainer, date("$on_date $start_time"), $status, $desc, $sess_id, $tech_id);

		}

		return $sessn;
	}

}
 
?>
